package com.buss.product.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.ResultList;
import com.buss.product.dao.GoodsPropDao;
import com.buss.product.model.GoodsProp;

@Repository
public class GoodsPropDaoImpl implements GoodsPropDao {

	@Autowired
	private BaseDao<GoodsProp> baseDao;
	
	@Override
	public GoodsProp saveGoodsProp(GoodsProp goodsProp) {
		return baseDao.saveOrEdit(goodsProp);
	}

	@Override
	public void editGoodsProp(GoodsProp goodsProp) {
		baseDao.edit(goodsProp);
	}

	@Override
	public void removeGoodsProp(Long[] ids) {
		String sql = "delete from GoodsProp goodsProp where goodsProp.id in (:ids)";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		baseDao.excuteHsql(sql, params);
	}


	@Override
	public void removeGoodsPropByGoodsId(Long goodsId) {
		String sql = "delete from GoodsProp goodsProp where goodsProp.goods.id = :goodsId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		baseDao.excuteHsql(sql, params);
	}
	
	@Override
	public GoodsProp loadGoodsProp(Long id) {
		return baseDao.get(id, GoodsProp.class);
	}

	@Override
	public GoodsProp loadGoodsProp(Long goodsId, Long catePropId) {
		String sql = "select goodsProp from GoodsProp goodsProp where " +
				"goodsProp.goods.id = :goodsId and goodsProp.cateProp.id = :catePropId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		params.put("catePropId", catePropId);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public GoodsProp loadGoodsProp(Long goodsId, String propCode) {
		String sql = "select goodsProp from GoodsProp goodsProp where " +
				"goodsProp.goods.id = :goodsId and goodsProp.cateProp.code = :propCode";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		params.put("propCode", propCode);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public GoodsProp loadGoodsProp(String goodsCode, Long propId) {
		String sql = "select goodsProp from GoodsProp goodsProp where " +
				"goodsProp.goods.code = :goodsCode and goodsProp.cateProp.id = :propId";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsCode", goodsCode);
		params.put("propId", propId);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public GoodsProp loadGoodsProp(String companyId, String goodsCode,
			String propCode) {
		String sql = "select goodsProp from GoodsProp goodsProp where " +
				" goodsProp.cateProp.propGroup.companyId = :companyId and " +
				" goodsProp.goods.code = :goodsCode and goodsProp.cateProp.code = :propCode";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("goodsCode", goodsCode);
		params.put("propCode", propCode);
		return baseDao.loadHsql(sql, params);
	}

	@Override
	public ResultList<GoodsProp> queryGoodsProp(Long goodsId) {
		String sql = "select goodsProp from GoodsProp goodsProp where goodsProp.goods.id = :goodsId " ;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsId", goodsId);
		return baseDao.queryHsql(sql, params, null);
	}

	@Override
	public ResultList<GoodsProp> queryGoodsProp(String companyId,String goodsCode) {
		String sql = "select goodsProp from GoodsProp goodsProp where goodsProp.goods.category.companyId = :companyId " +
				" and goodsProp.goods.code = :goodsCode ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		params.put("goodsCode", goodsCode);
		return baseDao.queryHsql(sql, params, null);
	}


}
